package com.pushtime.web.rest.search;

import com.pushtime.domain.HdfsLog;
import com.pushtime.repository.HdfsLogRepository;
import com.pushtime.service.search.HdfsDownloadService;
import com.pushtime.web.rest.vm.HdfsVM;
import com.pushtime.web.rest.vm.IdVM;
import io.github.jhipster.web.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Optional;

@RestController
@RequestMapping("/api/search")
public class HdfsDownloadResource {

    @Value("${application.webhdfs.blockSize}")
    private Integer LENGTH;

    private final HdfsDownloadService hdfsDownloadService;

    private final TaskExecutor taskExecutor;

    private final HdfsLogRepository hdfsLogRepository;

    public HdfsDownloadResource(HdfsDownloadService hdfsDownloadService, TaskExecutor taskExecutor, HdfsLogRepository hdfsLogRepository) {
        this.hdfsDownloadService = hdfsDownloadService;
        this.taskExecutor = taskExecutor;
        this.hdfsLogRepository = hdfsLogRepository;
    }

    @PostMapping("/read-files")
    public ResponseEntity<HdfsLog> createHdfsDownload(@RequestBody @Valid HdfsVM hdfsVM) {
        // 第一步，建立下载操作
        HdfsLog hdfsLog = hdfsDownloadService.createHdfsDownload(hdfsVM);
        // 第二步，开启线程执行下载,2 表示失败
        if (hdfsLog.getState() != 2) {
            taskExecutor.execute(() -> {
                try {
                    FileOutputStream fileOutputStream = new FileOutputStream(new File(hdfsLog.getDst()));
                    while (hdfsLog.getState().equals(1) &&
                        !(hdfsLog.getTotalSize()<= hdfsLog.getCurrentSize())) {
                        hdfsDownloadService.execute(hdfsLog, fileOutputStream);
                    }
                    fileOutputStream.close();
                } catch (IOException e) {
                    hdfsLog.setDesc(e.getMessage());
                    hdfsLogRepository.save(hdfsLog);
                }

            });
        }
        return ResponseEntity.ok().body(hdfsLog);
    }

    @GetMapping("/read-file-process")
    public ResponseEntity<HdfsLog> createHdfsDownload(@RequestParam Long id) {
        // 获取当前状态
        Optional<HdfsLog> hdfsLog = hdfsDownloadService.getStatus(id);
        return ResponseUtil.wrapOrNotFound(hdfsLog);
    }

    @PostMapping("/read-file-process")
    public ResponseEntity<HdfsLog> readProcess(@RequestBody @Valid IdVM idVM) {
        // 获取当前状态
        Optional<HdfsLog> hdfsLog = hdfsDownloadService.getStatus(idVM.getId());
        return ResponseUtil.wrapOrNotFound(hdfsLog);
    }


    @GetMapping("/read-files/synchronized")
    public ResponseEntity<HdfsLog> createSynchronizedHdfsDownload(@Valid HdfsVM hdfsVM) {
        HdfsLog hdfsLog = hdfsDownloadService.createSynchronizedHdfsDownload(hdfsVM);
        return ResponseEntity.ok().body(hdfsLog);
    }
}
